package org.lgiavedoni.android.franq.event.generation.phone;

import org.lgiavedoni.android.franq.core.listener.PhoneBroadcastListener;
import org.lgiavedoni.android.franq.service.communication.CommunicationService;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class BatteryListener extends PhoneBroadcastListener {
	private boolean alertLess50 = false;
	private boolean alertLess20 = false;

	public void run(Integer level) {
		if (level < 50 && !alertLess50) {
			CommunicationService.inst.sendDefaultTextMessage("Battery is less than 50% [" + level + "]");
			alertLess50 = true;
		}

		if (level < 20 && !alertLess20) {
			CommunicationService.inst.sendDefaultTextMessage("Battery is less than 20% [" + level + "]");
			alertLess20 = true;
		}
	}

	@Override
	public IntentFilter getListenetFilter() {
		return new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
	}

	@Override
	public void onReceive(Context arg0, Intent intent) {
		int level = intent.getIntExtra("level", 0);
		run(level);

	}

}
